package srv

import (
	dbModel "back/entity/model"
	"back/entity/nailEntity"
	"back/entity/nailRepo"
	requset "back/router/request"
	"github.com/redis/go-redis/v9"
	"gorm.io/gen"
	"utilx/dbx/cache"
)

type NailMemberCardLog struct {
	Repo   nailRepo.NailRepo
	bucket cache.RedisBucket
	redis  *redis.Client
}

func NewMemberCardLog(repo nailRepo.NailRepo, bucket cache.RedisBucket) NailMemberCardLog {
	client := bucket.GetClient(1)
	return NailMemberCardLog{repo, bucket, client}
}
func (c NailMemberCardLog) CacheSearchById(force bool, id int64) (any, error) {
	return cache.GetAop[nailEntity.NailMemberCardLog](force, c.redis, dbModel.RedisKey(dbModel.TypeMemberCardLog, id), func() (any, error) {
		return c.SearchById(id)
	})
}
func (c NailMemberCardLog) CacheSearchBy(force bool, rec requset.SearchWithPage[nailEntity.NailMemberCardLog]) (any, error) {
	return cache.GetAop[requset.PageResult[nailEntity.NailMemberCardLog]](force, c.redis, dbModel.RedisKey(dbModel.TypeMemberCardLog, rec), func() (any, error) {
		return c.SearchBy(rec)
	})

}

func (c NailMemberCardLog) SearchById(id int64) (*nailEntity.NailMemberCardLog, error) {

	return c.Repo.NailMemberCardLog.Where(nailRepo.NailMemberCardLog.ID.Eq(id)).First()
}
func (c NailMemberCardLog) SearchBy(rec requset.SearchWithPage[nailEntity.NailMemberCardLog]) (requset.PageResult[nailEntity.NailMemberCardLog], error) {
	do := c.Repo.NailMemberCardLog.Debug()
	do = do.Where(nailRepo.NailMemberCardLog.MemberName.Like("%" + rec.Data.MemberName + "%"))
	byPage, i, err := do.Order(nailRepo.NailMemberCard.CreatedAt.Desc()).FindByPage(int(rec.PageInfo.PageSize)*int(rec.PageInfo.Page-1), int(rec.PageInfo.PageSize))
	return requset.PageResult[nailEntity.NailMemberCardLog]{
		Items: byPage,
		Total: i,
	}, err
}
func (c NailMemberCardLog) DelById(id int64) (gen.ResultInfo, error) {

	return c.Repo.NailMemberCardLog.Where(nailRepo.NailMemberCardLog.ID.Eq(id)).Delete()
}
func (c NailMemberCardLog) UpdateById(rec nailEntity.NailMemberCardLog) error {

	return c.Repo.NailMemberCardLog.Where(nailRepo.NailMemberCardLog.ID.Eq(rec.ID)).Save(&rec)
}
func (c NailMemberCardLog) UpdateBy(rec requset.UpdateBy[nailEntity.NailMemberCardLog, nailEntity.NailMemberCardLog]) error {

	return c.Repo.NailMemberCardLog.Where(nailRepo.NailMemberCardLog.ID.Eq(rec.Where.ID)).Save(&rec.Target)
}
func (c NailMemberCardLog) Create(rec nailEntity.NailMemberCardLog) error {
	return c.Repo.NailMemberCardLog.Create(&rec)
}
